import {CSV} from "db://assets/scripts/common/CSV";
import {ChapterField} from "db://assets/scripts/data/db/ChapterField";
import {Chapter} from "db://assets/scripts/data/model/Chapter";

/**
 * 关卡数据转换器
 * public convertFromVector: (V) -> T
 * public convertToVector: (T) -> V
 */
export class ChapterConverter {
    /**
     * 禁止创建对象
     */
    private constructor() {
    }

    /**
     * 转数组
     *
     * @param csv CSV
     */
    public static convertFromCSV(csv: CSV): Chapter[] {
        let result: Chapter[] = [];
        csv.datas.forEach((row) => {
            result.push(this.convertFromRow(row));
        });
        return result;
    }

    /**
     * 转对象
     *
     * @param row CSV行数据
     */
    public static convertFromRow(row: string[]): Chapter {
        return {
            id: row[ChapterField.ID],
            mapName: row[ChapterField.MAP_NAME],
            attackAddition: Number(row[ChapterField.ATTACK_ADDITION]),
            defenseAddition: Number(row[ChapterField.DEFENSE_ADDITION]),
            hpAddition: Number(row[ChapterField.HP_ADDITION]),
            moveSpeedAddition: Number(row[ChapterField.MOVE_SPEED_ADDITION]),
            attackSpeedAddition: Number(row[ChapterField.ATTACK_SPEED_ADDITION]),
        };
    }
}